﻿package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;

import server.PersonInterface;
import server.ResCustomerInterface;
import server.ResDinnerInterface;
import server.ResMenuInterface;
import server.ResTableInterface;
import server.ResTakeOutInterface;
import server.ResWaiterInterface;
import client.Configuration;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.Color;




public class ResGiveMoneyGUI extends JDialog {

	private JPanel contentPane;
	
	private int k;
	private int n;
	
	private double amo;
	private int hp;

	/**
	 * Launch the application.
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					int a[]={1,2,3,4};
					ResGiveMoneyGUI frame = new ResGiveMoneyGUI(a);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 */
	/**
	 * Create the frame.
	 */
	public ResGiveMoneyGUI(int []a,int kind,int number) {
		
		k=kind;
		n=number;
		
		setTitle("\u7ED3\u8D26");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 359, 248);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		Configuration config= new Configuration();

		String rmiAddress=config.getRmiAddress();
		try {
			ResWaiterInterface waiter = (ResWaiterInterface) Naming.lookup(rmiAddress+"ResWaiter");
			
			amo=waiter.TolAmount(a);
			hp=(int) waiter.TolHp(a);
			String str=Double.toString(waiter.TolAmount(a));
			String str1=Double.toString(waiter.TolHp(a));
		
			
			
			JLabel label = new JLabel("\u60A8\u5171\u6D88\u8D39\u4E86\uFF1A"+":"+str+
					"   ");
			label.setForeground(new Color(128, 0, 0));
			label.setFont(new Font("华文行楷", Font.BOLD, 25));
		label.setBounds(95, 24, 236, 62);
		contentPane.add(label);
		
		JLabel lblNewLabel_1 = new JLabel("Hp:   "+str1);
		lblNewLabel_1.setForeground(new Color(128, 0, 0));
		lblNewLabel_1.setFont(new Font("华文行楷", Font.PLAIN, 25));
		lblNewLabel_1.setBounds(95, 111, 204, 38);
		contentPane.add(lblNewLabel_1);
		
} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		
		
		
		JButton btnOk = new JButton("\u786E\u8BA4");
		btnOk.setFont(new Font("方正舒体", Font.PLAIN, 18));
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Configuration config= new Configuration();
				String rmiAddress=config.getRmiAddress();
				
			try {				
				
				ResDinnerInterface dinner = (ResDinnerInterface) Naming.lookup(rmiAddress+"ResDinnerInfo");
				ResCustomerInterface cus=(ResCustomerInterface) Naming.lookup(rmiAddress+"ResCustomer");
				ResTableInterface table=(ResTableInterface)Naming.lookup(rmiAddress+"ResTable");
				PersonInterface person=(PersonInterface)Naming.lookup(rmiAddress+"Person");

						if(k==1){
							
									int t=dinner.getDinnerWaiter(n);
				
									int tableNum=dinner.getDinnerTable(n);

						
						dinner.setDinnerAmount(n, amo);						
						table.setTableState(n, 0);
						Person p=new Person();
						int id=p.getPID();
						cus.giveMoney(id, amo,hp);
						cus.leaveWaier(t);
						
				}
				else{
					ResTakeOutInterface takeOut = (ResTakeOutInterface) Naming.lookup(rmiAddress+"ResTakeOutInfo");
					takeOut.addNewOrder2(n,amo);
					Person p=new Person();
					int id=p.getPID();
					cus.giveMoney(id, amo,hp);

					
					
				}
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			
			dispose();
			}			
		});
		btnOk.setBounds(204, 159, 93, 23);
		contentPane.add(btnOk);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon("src/client/images/gif_45_034.gif"));
		lblNewLabel.setBounds(36, 22, 49, 69);
		contentPane.add(lblNewLabel);
		

		
		
	}
}
